function varargout = racato(varargin)
% RACATO (RAMAN Calibration Tool)
%
%      RACATO offers a GUI which enables the user to load either a SIF- or
%      MAT-type file for LASER wavelength calibration for Raman
%      instruments.
%      If a SIF-type file is choosen, the file will be converted to MAT
%      format prior to processing. The MAT-file will be saved in the same
%      directory as the original data.
%
%      Currently, the following kind of reference samples are supported:
%
%           - Pure Silicone
%           - Cyclohexane
%           - Polystyrene
%           - Paracetamol (4-acetamidophenol)
%
% See also: cyclohexcalibration, sicalibration, paracetamolcalibration,
%               loadsif_nonGUI
%
% v1.3, 27-Sep-2012
% v1.2, 07-Sep-2011

% Last Modified by GUIDE v2.5 15-Jul-2011 13:47:00

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @racato_OpeningFcn, ...
                   'gui_OutputFcn',  @racato_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before racato is made visible.
function racato_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to racato (see VARARGIN)

% Choose default command line output for racato
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes racato wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = racato_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;


% --- Executes on button press in loaddata.
function loaddata_Callback(hObject, eventdata, handles)
%% ============= CONFIG =============

versionno = '1.2';
buildno = '20110907';


%% open UIGETFILE to choose datafile for calibration
[filename,filepath,ext] = uigetfile(...
  {'*.mat','MAT-formated APE Files (*.mat)';
   '*.sif','ANDOR SIF raw data (*.sif)'; ...
   '*.*',  'All Files (*.*)'}, ...
   'Pick a file');
if isequal(filename,0)
   disp('User selected Cancel')
else
   disp(['User selected ', fullfile(filepath, filename)])
end

%% construct compatible filename
[~, name, ext] = fileparts(filename);
datasetname = fullfile(filepath,name);
logfilename = ['log-RaCaTo_',date,'_',name,'.txt'];

%% update GUI with filename
set(handles.loadsuccess, 'String', [name, ' loaded.']);

%% detect id SIF conversion is necessary
comp = strcmp(ext, '.sif');
if comp == 1 
    loadsif_nonGUI(datasetname,1)
end

%% Analyse sample by type choosen in GUI
if get(handles.sitype,'value') == 1
    sampletype='Silicon    ';
    LaserWL=sicalibration(datasetname);
    ramanpeak = 'There can be only one!TM';
elseif get(handles.cyclotype,'value') == 1
    [LaserWL, ramanpeak, LaserWLgauss]=cyclohexcalibration(datasetname);
    sampletype='Cyclohexan ';
elseif get(handles.polytype,'value') == 1
    [LaserWL, ramanpeak, LaserWLgauss]=polystyrenecalibration(datasetname);
    sampletype='Polystyrene';
elseif get(handles.paratype,'value') == 1
    [LaserWL, ramanpeak, LaserWLgauss]=paracetamolcalibration(datasetname);
    sampletype='Paracetamol';
else
    errordlg({'Something with the sample type', 'went very wrong. Sorry.'})
    return
end

if LaserWL ~= 0 
    set(handles.resulttext,'String','Laser Wavelength is ')
    set(handles.resultWL,'String',[num2str(LaserWL),' nm'])
else
    errordlg('Laser wavelength could not be determined');
end

%% Output Log file
logname = fullfile(filepath,logfilename);
diary(logname);          % turns logging to log-file on
disp([' ================= ',datestr(now),' ================='])
disp(' ======                                            ======')
disp([' ======   The RaCaTo, Version ',num2str(versionno),', Build', ...
    num2str(buildno),'   ======'])
disp(' ======                                            ======')
disp([' ========================================================'])
disp(' ==                                                    ==')
disp(' == Filename:                                          ==')
disp([' ==   ', datasetname])
disp(' ==                                                    ==')
disp([' == Sample Type: ',sampletype,'                           =='])
disp([' == Choosen Peak: ',num2str(ramanpeak), ...
    ' cm^-^1                        =='])
disp(' ==                                                    ==')
disp([' == Calulated Laser Wavelength: ',num2str(LaserWL), ...
    ' nm            =='])
if  get(handles.cyclotype,'value') == 1
    disp([' == Gauss fitted Laser Wavelength: ',num2str(LaserWLgauss), ...
    ' nm         =='])
end
disp(' ==                                                    ==')
disp([' ========================================================'])
disp(' '), disp(' '),disp(' '), disp(' '), disp(' ')
diary off                                               % suspend Logging


% --- Executes on button press in helpbutton.
function helpbutton_Callback(hObject, eventdata, handles)
h = msgbox(help('racato'),'The RaCaTo', 'help', 'modal');
uiwait(h)


% --- Executes on button press in resetbutton.
function resetbutton_Callback(hObject, eventdata, handles)
delete(handles.figure1);
clear variables;
racato

% --- Executes on button press in exitbutton.
function exitbutton_Callback(hObject, eventdata, handles)
close
